package com.timothy.junk.common.parse;

import android.content.Context;
import android.content.res.XmlResourceParser;
import org.xmlpull.v1.XmlPullParserException;

import com.timothy.junk.R;
import com.timothy.junk.gfund.bean.GFundBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**

 */
public class ParseXML {
 
    public static List<GFundBean> getGFundList(Context context) {
        List<GFundBean> rList = new ArrayList<GFundBean>();
        XmlResourceParser xrp = context.getResources().getXml(R.xml.funds);
        try {
        	GFundBean gFund = null;
            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
                    String tagName = xrp.getName();
                    if (tagName.equals("gfund")) {
                        gFund = new GFundBean();
                    }
                    if(tagName.equals("index") && gFund !=null){
                        gFund.setIndex(xrp.nextText());
                    }
                    
                    if(tagName.equals("funda_code") && gFund !=null){
                        gFund.setFundACode(xrp.nextText());
                    }
                    if(tagName.equals("funda_name") && gFund !=null){
                        gFund.setFundAName(xrp.nextText());
                    }
                    if(tagName.equals("funda_share") && gFund !=null){
                        gFund.setFundAShare(Integer.valueOf(xrp.nextText()));
                    }
                    
                    if(tagName.equals("fundb_code") && gFund !=null){
                        gFund.setFundBCode(xrp.nextText());
                    }
                    if(tagName.equals("fundb_name") && gFund !=null){
                        gFund.setFundBName(xrp.nextText());
                    }
                    if(tagName.equals("fundb_share") && gFund !=null){
                    	gFund.setFundBShare(Integer.valueOf(xrp.nextText()));
                    }
                    
                    if(tagName.equals("fundm_code") && gFund !=null){
                        gFund.setFundMCode(xrp.nextText());
                    }
                    if(tagName.equals("fundm_name") && gFund !=null){
                        gFund.setFundMName(xrp.nextText());
                    }
                    if(tagName.equals("fundm_index_code") && gFund !=null){
                        gFund.setFundMIndexCode(xrp.nextText());
                    }
                    if(tagName.equals("fundm_index_ratio") && gFund !=null){
                        gFund.setFundMIndexRatio(Double.valueOf(xrp.nextText()));
                    }
                }
                if(xrp.getEventType() == XmlResourceParser.END_TAG){
                    String tagName = xrp.getName();
                    if (tagName.equals("gfund")) {
                        rList.add(gFund);
                    }
                }
                xrp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
        	if(xrp!=null) xrp.close();
        }
        
        return rList;
    }
    
    public static GFundBean getGFundByIndex(Context context,String searchIndex) {
    	GFundBean gFund = null;
    	String currentIndex = null;
        XmlResourceParser xrp = context.getResources().getXml(R.xml.funds);
        try {
        	
            while (xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
                if (xrp.getEventType() == XmlResourceParser.START_TAG) {
                    String tagName = xrp.getName();
                    if (tagName.equals("gfund")) {
                        gFund = new GFundBean();
                    }
                    
                    if(tagName.equals("index") && gFund !=null){
                    	currentIndex = xrp.nextText();
                    	gFund.setIndex(currentIndex);
                    }
                    
                    if(gFund !=null){
                        if(tagName.equals("funda_code") ){
                            gFund.setFundACode(xrp.nextText());
                        }
                        if(tagName.equals("funda_name") ){
                            gFund.setFundAName(xrp.nextText());
                        }
                        if(tagName.equals("funda_share") ){
                            gFund.setFundAShare(Integer.valueOf(xrp.nextText()));
                        }
                        
                        if(tagName.equals("fundb_code") ){
                            gFund.setFundBCode(xrp.nextText());
                        }
                        if(tagName.equals("fundb_name") ){
                            gFund.setFundBName(xrp.nextText());
                        }
                        if(tagName.equals("fundb_share") ){
                        	gFund.setFundBShare(Integer.valueOf(xrp.nextText()));
                        }
                        
                        if(tagName.equals("fundm_code") ){
                            gFund.setFundMCode(xrp.nextText());
                        }
                        if(tagName.equals("fundm_name") ){
                            gFund.setFundMName(xrp.nextText());
                        }
                        if(tagName.equals("fundm_index_code") ){
                            gFund.setFundMIndexCode(xrp.nextText());
                        }
                        if(tagName.equals("fundm_index_ratio") ){
                            gFund.setFundMIndexRatio(Double.valueOf(xrp.nextText()));
                        }                   	
                    }
                    

                }
                if(xrp.getEventType() == XmlResourceParser.END_TAG){
                    String tagName = xrp.getName();
                    if (tagName.equals("gfund") && gFund !=null) {
                    	if(currentIndex.equals(searchIndex)){
                    		return gFund;
                    	}else{
                    		gFund = null; 
                    	}
                        
                    }
                }
                xrp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
        	if(xrp!=null) xrp.close();
        }
        return gFund;
    }


}

